//
//  LFPictureViewCell.swift
//  选取相册
//
//  Created by 刘健 on 16/4/4.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit


protocol LFPictureViewCellDelegate: NSObjectProtocol {
    
    func didClickAddButton(cell:LFPictureViewCell,button:UIButton)
    
    func didClickDeleteButton(cell:LFPictureViewCell,button:UIButton)
}

class LFPictureViewCell: UICollectionViewCell {
    
    weak var delagate: LFPictureViewCellDelegate?
    
      //MARK: --  显示图片
    
    var image: UIImage? {
        didSet {
             addButton.setImage(image, forState: UIControlState.Normal)
            
            deleteButton.hidden = false
        }
    }
    
    //当图片达到最大的数时，显示加号
    func setupAddButton() {
        
        addButton.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        addButton.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: UIControlState.Selected)
        
        deleteButton.hidden = true
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.brownColor()
        
        prepareUI()
    }
    
    private func prepareUI() {
        
        contentView.addSubview(addButton)
        contentView.addSubview(deleteButton)
        
        addButton.translatesAutoresizingMaskIntoConstraints = false
        deleteButton.translatesAutoresizingMaskIntoConstraints = false
        
        //VFL布局
    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[add]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["add" : addButton]))
        
   contentView.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[add]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["add" : addButton]))
        
    contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-56-[delete]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["delete" : deleteButton]))
    
    contentView.addConstraints( NSLayoutConstraint.constraintsWithVisualFormat("V:|-2-[delete]-53-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["delete" : deleteButton]))

    }
    
      //MARK: -- 按钮点击事件
    
    @objc private func didClickAdd() {
        
        delagate?.didClickAddButton(self, button: addButton)
    }
    
    @objc private func didClickDelete() {
        
        delagate?.didClickDeleteButton(self, button: deleteButton)
    }
    
      //MARK: -- 懒加载
    private lazy var addButton:UIButton = {
       let button = UIButton()
        button.setImage(UIImage(named: "compose_pic_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: UIControlState.Selected)
        
        //处理照片平铺
        button.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
        
        button.addTarget(self, action: "didClickAdd", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didClickDelete", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
